﻿/// <reference path="../Jquery/jquery-1.2.6.js" />
/// <reference path="../Core.js" />

IM.MessageManager = function() {
    IM.Base.apply(this, arguments);

    //发送消息的回调
    this.SendMessageCallBack = function(data) {
        if (data.Result) {
            //发送成功 data = {Result:true}
        }
        else {
            //发送失败 data = {Result:true , Message:message}
            //通知对应好友窗口，显示失败
        }
    };

    //发送消息
    this.SendMessage = function(SenderId, ReceiverId, Content, MessageType) {
        if (arguments.length == 1)
            $.post("SendMessage", msg, this.SendMessageCallBack, "json");
        else
            $.post("SendMessage", this.CreateMessage(SenderId, ReceiverId, Content, MessageType), this.SendMessageCallBack, "json");

    };

    //接收到消息时的回调
    this.ReceiveMessageCallBack = function(data) {
        if (data.Result) {//如果接收到消息,对消息进行分类
            var newMsgs = data.Messages;
            for (var i = 0; i < newMsgs.length; i++) {
                switch (newMsgs[i].MessageType) {
                    case IM.InstantMessageType.PersonalMessage: //获取到个人消息
                        var friend = IM.Chat.FriendManager.FindFriendById(newMsgs[i].SenderId); //找到消息的发送人
                        if (friend != null) {//如果存在该好友，则将消息存储在该好友对象中
                            if (typeof friend.Messages == "undefined") {
                                friend.Messages = new IM.Utility.ArrayList();
                            }
                            friend.Messages.Add(newMsgs[i]);
                            var win = IM.Chat.ChatWindows.Find(friend.UserId);
                            if (win != null) {
                                if (win.GetVisible()) {
                                    win.ShowMessage(); //聊天窗口显示最新消息
                                }
                            }
                            IM.Chat.FriendManager.RefreshUI(); //刷新好友列表,更新好友消息数量
                        }
                        else {//则为陌生人消息,待实现

                        }
                        break;
                    case IM.InstantMessageType.GroupMessage: //群消息,待实现

                        break;
                    case IM.InstantMessageType.SystemMessage: //系统消息
                        IM.Chat.SystemMessages.Add(newMsgs[i]);
                        break;
                }
            }

        }
    };

    this.ReceiveMessage = function() {//接收消息
        $.post("ReceiveMessage", null, this.ReceiveMessageCallBack, "json");
    };

    this.CreateMessage = function(SenderId, ReceiverId, Content, MessageType) {//创建消息
        var msg = {};
        msg["SenderId"] = SenderId;
        msg["ReceiverId"] = ReceiverId;
        msg["Content"] = Content;
        msg["MessageType"] = MessageType;
        return msg;
    };

    //定时取消息
    this.StartInterval = function() {
        IM.SetInterval(3000, 0, (function(obj) { return function() { obj.ReceiveMessage(); }; })(this));
    };
};